package com.luoying.itew.common;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.provider.Settings;
import android.util.Log;

import androidx.multidex.MultiDex;

import com.facebook.stetho.Stetho;
import com.luoying.itew.BuildConfig;
import com.luoying.itew.db.DBManager;
import com.luoying.itew.manage.UserManage;
import com.luoying.itew.util.LogUtils;
import com.tencent.bugly.crashreport.CrashReport;

import cn.jpush.android.api.JPushInterface;


public class AntaiApplication extends Application {
    public static Context context;
    public static String android_id;
    public static int cuurMonitorId;//当前监测点ID

    @Override
    public void onCreate() {
        super.onCreate();
        if(!getProcess().equals(getPackageName())){
            return;
        }
        MultiDex.install(this);
        context = this;
        android_id = getAndroidId();
        LogUtils.init(BuildConfig.DEBUG);
        CrashReport.initCrashReport(getApplicationContext(), Constant.BUGLY_APP_ID, BuildConfig.DEBUG);
        if(BuildConfig.DEBUG){
            Stetho.initializeWithDefaults(this);
        }
        startJPush();
        DBManager.getInstance().creatDB();
        UserManage.getInstance().initUser();
    }

    public String getAndroidId(){
        return Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
    }

    /**
     * 初始化极光推送
     */
    private void startJPush(){
        Runnable runnable = () -> {
            JPushInterface.init(context);
            JPushInterface.setDebugMode(BuildConfig.DEBUG);
        };
        new Thread(runnable).start();
    }

    /**
     * 获取当前进程的名称
     * @return
     */
    private String getProcess(){
        ActivityManager activityManager = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);
        int pid = android.os.Process.myPid();
        String processName ="";
        for (ActivityManager.RunningAppProcessInfo info:activityManager.getRunningAppProcesses()){
            if(info!=null&&pid==info.pid){
                processName = info.processName;
                break;
            }
        }
        return processName;
    }

}
